package com.lt.flashcard.view;

import java.util.Arrays;
import java.util.Set;

import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.util.Clients;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.entity.Medal;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.EmailService;
import com.lt.flashcard.model.service.FlashcardSetService;
import com.lt.flashcard.model.service.LearningService;
import com.lt.flashcard.model.service.Repository;

public class CommonCtrl {

    private String email = "";
    private String message = "";
    private String link = "";
    private String shareId = "";
    private String shareType = "";

    @Init
    public void init() {

    }

    @Command
    public void shareEmail() {
        if (!Utility.isLogin()) {
            Clients.evalJavaScript("showNotice('', 'Bạn phải đăng nhập để sử dụng chức năng này')");
            return;
        }
        if (Utility.isEmptyString(email) || Utility.isEmptyString(message) || Utility.isEmptyString(shareId)) {
            Clients.evalJavaScript("showNotice('', 'Bạn chưa nhập đầy đủ thông tin')");
            return;
        }

        if (Utility.isEmptyString(shareType))
            shareType = "fcs";

        // TODO check various error here

        Set<String> val = Utility.filterValidEmails(Arrays.asList(email.split(",")));
        if (val.size() > 0) {
            User user = Utility.getUser();
            EmailService srv = Repository.getInstance().getEmailService();

            if (shareType.equals("fcs")) { // share flashcardset
                FlashcardSetService fcsrv = Repository.getInstance().getFlashcardSetService();
                FlashcardSet set = fcsrv.findByID(Utility.parseLong(shareId));
                for (String email : val)
                    srv.sendEmailShareSet(user, email, set);
            } else if (shareType.equals("medal")) { // share medal
                LearningService msrv = Repository.getInstance().getLearningService();
                Medal medal = msrv.getMedal(shareId);
                for (String email : val)
                    srv.sendEmailShareMedal(user, email, medal);
            }

            /*
             * email = ""; message = ""; shareType = "fcs"; BindUtils.postNotifyChange(null, null, this, "email");
             * BindUtils.postNotifyChange(null, null, this, "message"); BindUtils.postNotifyChange(null, null, this,
             * "shareType");
             */

            Clients.evalJavaScript("$.fancybox.close()");
        }
 else {
            Clients.evalJavaScript("showNotice('', 'Email không hợp lệ')");
        }
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    /**
     * @return the shareId
     */
    public String getShareId() {
        return shareId;
    }

    /**
     * @param shareId
     *            the shareId to set
     */
    public void setShareId(String shareId) {
        this.shareId = shareId;
    }

    /**
     * @return the shareType
     */
    public String getShareType() {
        return shareType;
    }

    /**
     * @param shareType
     *            the shareType to set
     */
    public void setShareType(String shareType) {
        this.shareType = shareType;
    }

}
